104. 二叉树的最大深度
https://leetcode.cn/problems/maximum-depth-of-binary-tree/
- 提交时间:2023-02-21 12:35:02
- 执行用时:40 ms, 在所有 Python3 提交中击败了90.92%的用户
- 内存消耗:17.6 MB, 在所有 Python3 提交中击败了5.14%的用户
- 通过测试用例:39 / 39
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if None == root:
return 0
if root.left == None and root.right == None:
return 1
return self.maxDepthx(1, root)
def maxDepthx(self, length, child: TreeNode):
if None == child:
return length
if None == child.left and None == child.right:
return length
return max(self.maxDepthx(length + 1, child.left), self.maxDepthx(length + 1, child.right))